package com.px.gis.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.px.gis.entity.Area;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface AreaMapper extends BaseMapper<Area> {

    @Update("update area set polygon = ST_GeomFromEWKT('SRID=4326;POLYGON((${polygon}))') where area_id=#{id}")
    void updatePolygon(Long id, String polygon);

    /**
     * @param name      名称
     * @param longitude 经度
     * @param latitude  纬度
     * @param radius    半径(米)
     */
    @Update("update area set polygon = ST_SetSRID(ST_Buffer(ST_MakePoint(#{longitude}, #{latitude})::geography,#{radius})::geometry,4326) where area_id=#{id}")
    void updateCircle(Long id, Double longitude, Double latitude, Double radius);


    @Select("select id_path from area" +
            " where ST_Intersects(polygon,ST_GeomFromEWKT('SRID=4326;POLYGON((${polygon}))'))"
    )
    List<Area> intersects(@Param("polygon") String polygon);

    @Select("select area_id, parent_id, id_path, name, type, address, city, color, country, data_source, description, height, lat, lng, level, polygon, radius, shape, url, begin_at, end_at, external, st_asgeojson(polygon) as polygonJson from area" +
            " where ${idPath}")
    List<Area> likeRightByIdPath(String idPath);

}